﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Frame.Lib.Core.Tree;
using Frame.Lib.Core.User;
using Frame.Lib.Enums;

namespace Frame.Lib.Core.Dept
{
    public class DeptBO : TreeBO<DeptInfo>
    {
        private DeptDAL _dept;
        public DeptBO()
            : base(STableName.Dept)
        {
            _dept = new DeptDAL();
        }

        public override List<DeptInfo> GetTreeList(string webAppPath)
        {
            List<DeptInfo> list = base.GetTreeList(webAppPath);
            List<DeptUser> lstDeptUser = _dept.GetUserCount().ToList();

            foreach (DeptInfo info in list)
            {
                IEnumerable<int> lstSubDeptIDs = from dept in list
                                                 where dept.Path.Contains("," + info.ID + ",")
                                                 select dept.ID;

                info.UserCount = lstDeptUser.Count(p => lstSubDeptIDs.Contains(p.DeptID));
            }

            return list;

         
        }
    
        public IEnumerable<UserInfo> GetUserList(List<int> lstDeptIDs)
        {
            List<int> lstSubIDs = GetSubIDs(lstDeptIDs);           
            IEnumerable<UserInfo> list = _dept.GetUserList(lstSubIDs);
            return list;
        }


        public IEnumerable<UserInfo> GetUserList(int deptID)
        {
            List<int> lstSubIDs = GetSubIDs(deptID);
            IEnumerable<UserInfo> list = _dept.GetUserList(lstSubIDs);
            return list;
        }

        #region 合并部门用户
        /// <summary>
        /// 合并部门用户名称
        /// </summary>
        /// <param name="lstDeptNames"></param>
        /// <param name="lstUserNames"></param>
        /// <returns></returns>
        public static string MergeDeptUser(List<string> lstDeptNames, List<string> lstUserNames)
        {
            StringBuilder sbDeptUsers = new StringBuilder();

            foreach (string str in lstDeptNames)
            {
                sbDeptUsers.Append("," + str);
            }

            foreach (string str in lstUserNames)
            {
                sbDeptUsers.Append("," + str);
            }

            if (sbDeptUsers.Length > 0)
            {
                sbDeptUsers.Remove(0, 1);
            }
            return sbDeptUsers.ToString();
        }

        /// <summary>
        /// 合并部门用户ID
        /// </summary>
        /// <param name="lstDeptIDs"></param>
        /// <param name="lstUserIDs"></param>
        /// <returns></returns>
        public static string MergeDeptUser(List<int> lstDeptIDs, List<int> lstUserIDs)
        {
            StringBuilder sbDeptUsers = new StringBuilder();
            if (lstDeptIDs != null)
            {
                foreach (int id in lstDeptIDs)
                {
                    sbDeptUsers.Append("," + id);
                }
            }
            foreach (int id in lstUserIDs)
            {
                sbDeptUsers.Append(",-" + id);
            }
          
            return sbDeptUsers.ToString().TrimStart(',');
        }
        #endregion

        #region 分隔部门用户
        /// <summary>
        /// 分隔部门用户ID
        /// </summary>
        /// <param name="strDeptUserIDs"></param>
        /// <param name="lstDeptIDs"></param>
        /// <param name="lstUserIDs"></param>
        public static void SplitDeptUser(string strDeptUserIDs, List<int> lstDeptIDs, List<int> lstUserIDs)
        {
            if (strDeptUserIDs == null)
            {
                return;
            }
            string[] strIDs = strDeptUserIDs.Split(',');
            if (lstDeptIDs == null)
            {
                lstDeptIDs = new List<int>();
            }
            else
            {
                lstDeptIDs.Clear();
            }
            if (lstUserIDs == null)
            {
                lstUserIDs = new List<int>();
            }
            else
            {
                lstUserIDs.Clear();
            }

            foreach (string id in strIDs)
            {
                if (id != "")
                {
                    int idTmp = int.Parse(id);
                    if (idTmp <= 0)
                    {
                        lstUserIDs.Add(Math.Abs(idTmp));
                    }
                    else
                    {
                        lstDeptIDs.Add(idTmp);
                    }
                }
            }
        }
        #endregion

        public List<int> GetUserIDs(List<int> lstDeptIDs)
        {
            List<int> lstSubDeptIDs = base.GetSubIDs(lstDeptIDs);
            List<int> lstUserIDs = _dept.GetUserIDs(lstSubDeptIDs);
            return lstUserIDs;
           
        }
    }
}
